var Task;

/***************************************************************
 *
 *   Tasklist
 *
 ***************************************************************/
(Task = function (task_data, tasklist) { 
    elem = document.createElement('li');
    elem.id = task_data.id;
    elem.task = this;

    this.init(elem, task_data, tasklist);
})
.prototype = {
    init: function(elem, task_data, tasklist) {

        this.elem = elem;
        this.tasklist = tasklist;
        this.data = task_data;

        this.label = this.createTaskLabel(this.data.title);
        this.dateBox = this.createDateBox(tasklist.color());
        this.tasksBox = this.createTasksBox();

        this.setStyleElement();
    },

    setStyleElement: function() {
        var task = this;
        task.elem.style.textDecoration = "none";
        task.elem.onclick = function(evt) {
            doubleclick(this, evt, function () {
                task.update_subtask();
                obs.fireEvent(task.elem.style.textDecoration, task);
            }, function () {});
        };

        this.elem.appendChild(this.dateBox);
        this.elem.appendChild(this.label);
        this.elem.appendChild(this.tasksBox);
    },

    previous: function() {
        return (this.elem.previousSibling) ? this.elem.previousSibling.id : null;
    },

    update_subtask: function() {
        var subtasks = this.elem.getElementsByTagName('li');
        for(var i=0; i<subtasks.length; i++) {
            obs.fireEvent(this.elem.style.textDecoration, subtasks[i].task);
        }
    },

    createTaskLabel: function(title) {
        var task = this;
        var label = createEditableLabel(title, function() {
            if(label.value != task.data.title) {
                task.data.title = label.value;
                task.request_update();
            }
            label.blur();
        });
        return label;
    },

    createDateBox: function(color) {
        var task = this;
        var datebox = document.createElement('input');
        var invert_color = invert(color);

        datebox.className = "datebox";
        datebox.onmouseover = function () {
            datebox.style.background = color;
            datebox.style.color = invert_color;
        };
        datebox.onmouseleave = function () {
            datebox.style.background = document.body.background;
            datebox.style.color = color;
        };
        datebox.onfocus = function () { this.blur(); };
        datebox.style.readOnly = true;
        datebox.style.color = color;

        if(task.data.due)
        {
            var due = task.data.due;
            datebox.value = due.replace(/^[0-9]{4}-/, "").replace(/T.*/, "");
        }

        datebox.onclick = function(evt) {
            evt.stopPropagation();
            var datebox = this;
            $("#datepicker").datepicker("option",{
                dateFormat: 'mm-dd',
                onSelect: function (dateText, inst) {
                    if(datebox.value != dateText) {
                        var newDate = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay);
                        task.updateDate(dateText, newDate);
                    }
                    $(this).hide();
                }
            }).show();
        }

        return datebox;
    },

    createTasksBox: function() {
        var tasklist = this.tasklist;
        var ul = document.createElement('ul');
        ul.style.display = "block";
        ul.addTask = function(task) {
            this.appendChild(task);
            tasklist.addCount();
        }

        return ul;
    },

    updateDate: function(strdate, date) {
        var due = getFormatedDate(date) + "T00:00:00.000Z";
        this.data.due = due;
        this.dateBox.value = strdate;
        this.request_update();
    },

    complete: function() {
        var task = this;
        task.elem.style.textDecoration = "line-through";
        task.data.status = "completed";

        task.label.ondblclick = function(evt) {
            task.request_delete();
        }
        task.request_update();
    },

    uncomplete: function() {
        this.elem.style.textDecoration = "none";
        this.data.status = "needsAction";
        delete this.data.completed;

        this.label.ondblclick = toEditablebox;
        this.request_update();
    },

    request_insert: function() {
        var req_data = {
            tasklist: this.tasklist.id(),
            previous: this.previous(),
            resource: this.data
        };

        var task = this;
        var req = gapi.client.tasks.tasks.insert(req_data);
        console.log("insert : " + this.tasklist.title + " / " + this.data.due);
        //console.log(req_data);
        req.execute(function (res) {
            //console.log(res.result);
            task.data = res.result;
            task.elem.id = task.data.id;
        });
    },

    request_update: function() {
        var req_data = {
            tasklist: this.tasklist.id(),
            task: this.data.id,
            resource: this.data
        };

        var task = this;
        var req = gapi.client.tasks.tasks.update(req_data);
        console.log("update : " + this.data.title + "(" + this.data.status + ")");
        //console.log(req_data);
        req.execute(function (res) {
            //console.log(res);
            task.data = res.result;
        });
    },

    request_delete: function() {
        var req_data = {
            tasklist: this.tasklist.id(),
            task: this.data.id
        };

        var task = this;
        var req = gapi.client.tasks.tasks.delete(req_data);
        console.log("delete : " + this.data.title + "(" + this.data.status + ")");
        //console.log(req_data);
        req.execute(function (res) {
            //console.log(res);
            task.elem.removeChild(task.tasksBox);
            task.elem.removeChild(task.label);
            task.elem.removeChild(task.dateBox);
            task.elem.parentNode.removeChild(task.elem);
            task.tasklist.delCount();

            delete task.tasksBox;
            delete task.label;
            delete task.dateBox;
            delete task.elem;
            delete task;
        });
    },
};

Task.newItem = function() {
    var dueDate = getFormatedDate(new Date())+"T00:00:00.000Z";

    return {
        title: "new Item",
        due: dueDate,
        status: "needsAction",
    };
};

Task.getTasksBox = function(id) {
    return document.getElementById(id).task.tasksBox;
};

Task.setEventListener = function(obs) {
    obs.listen("line-through", Task.update_uncomplete);
    obs.listen("none", Task.update_complete);
};

Task.update_complete = function(observable, eventType, task) {
    task.complete();
};

Task.update_uncomplete = function(observable, eventType, task) {
    task.uncomplete();
};

